package com.yanfan.energy.controller;



import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.yanfan.common.utils.DateUtils;
import com.yanfan.energy.domain.IbmsEnergyDevice;
import com.yanfan.energy.domain.IbmsEnergyDeviceData;
import com.yanfan.energy.domain.IbmsEnergyFlow;
import com.yanfan.energy.mapper.IbmsEnergyDeviceDataMapper;
import com.yanfan.energy.mapper.IbmsEnergyDeviceMapper;
import com.yanfan.energy.service.IIbmsEnergyDeviceDataService;
import com.yanfan.energy.service.IIbmsEnergyFlowService;
import com.yanfan.energy.util.HttpGetUtil;
import com.yanfan.energy.vo.EnergyFlowRecord;
import com.yanfan.ibms.common.utils.HttpUtils2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;




//@Component("energy")
@RestController
public class ApiEnergyController {

    @Autowired
    IbmsEnergyDeviceMapper ibmsEnergyDeviceMapper;

    @Autowired
    IIbmsEnergyDeviceDataService ibmsEnergyDeviceDataService;

    @Autowired
    IbmsEnergyDeviceDataMapper ibmsEnergyDeviceDataMapper;

    @Autowired
    IIbmsEnergyFlowService ibmsEnergyFlowService;


    /**
     * 获取能耗设备信息
     */
    public void getParkingLotInfo() {

        HashMap<String, Object> map = new HashMap<>();
        map.put("stationNo", "stationNo");

        JSONObject jsonObject = HttpUtils2.sendPostBody("http://10.21.10.52:8005/api/v1/device/function/history-data", null, map);
        List<IbmsEnergyDevice> list = jsonObject.getList(jsonObject.toJSONString("parkingLotInfo"), IbmsEnergyDevice.class);
        //批量更新或新增到数据库
        ibmsEnergyDeviceMapper.insertOrUpdateBatch(list);
    }

    /**
     * 获取能耗设备数据信息
     */
    public void getEnergyDeiveData() {

        List<IbmsEnergyDevice>selectDeviceList=ibmsEnergyDeviceMapper.selectDeviceList();
        ArrayList<String> flows = new ArrayList<>();
        for (IbmsEnergyDevice ibmsEnergyDevice : selectDeviceList) {
            flows.add(ibmsEnergyDevice.getEnergyDeviceNumber());
        }

        List<String> groupedData = HttpGetUtil.groupData(flows);
        List<String> stringList = new ArrayList<>();
        for (String group : groupedData) {
            stringList.add(group);
        }
        System.out.println(groupedData);
        // 发送POST请求
        String url = "http://10.21.10.52:8004/token";
        HashMap<String, String> headers = new HashMap<>();
        headers.put("Content-Type","application/x-www-form-urlencoded");
        HashMap<String, Object> bodys = new HashMap<>();
        bodys.put("client_id","d8ulkdi8urdy4z69");
        bodys.put("client_secret","s590ddd0fc06228d79987f0881499064");
        bodys.put("grant_type","client_credentials");

        JSONObject jsonOb = HttpUtils2.sendPost(url,bodys,headers);
        String token = jsonOb.getString("access_token");
        System.out.println(token);
        for (String s : stringList) {
            JSONObject jsonObject = HttpGetUtil.sendGetRequest(
                    "http://10.21.10.52:8005/api/v1/device/functions?deviceIds="+s,token);
            if(jsonObject == null){
                return;
            }
            JSONArray json = jsonObject.getJSONArray("data");

            // 遍历JSON数组
            List<IbmsEnergyDeviceData> list = new ArrayList<>() ;
            for (int i = 0; i < json.size(); i++) {
                JSONObject deviceObj = json.getJSONObject(i);
                String deviceId = deviceObj.getString("deviceId");
                JSONArray functions = deviceObj.getJSONArray("functions");
                for (int j = 0; j < functions.size(); j++) {
                    JSONObject functionObj = functions.getJSONObject(j);
                    String identifier = functionObj.getString("identifier");
                    String value = functionObj.getString("value");
                    String time = functionObj.getString("time");
//                    System.out.println("Device ID: " + deviceId + ", Identifier: " + identifier + ", Value: " + value + ", Time: " + time);
                    if ("11X".equals(functionObj.getString("identifier"))) {
                        IbmsEnergyDeviceData data = new IbmsEnergyDeviceData();
                        data.setId(deviceId);
                        data.setIdentifier("11X");
                        data.setValue(value);
                        data.setTime(functionObj.getString("time"));
                        list.add(data);
                    }else if("12L".equals(functionObj.getString("identifier"))){
                        IbmsEnergyDeviceData data = new IbmsEnergyDeviceData();
                        data.setId(deviceId);
//                        data.setId(deviceId);
                        data.setIdentifier("12L");
                        data.setValue(value);
                        data.setTime(functionObj.getString("time"));
                        list.add(data);
                    }else if("12N".equals(functionObj.getString("identifier"))){
                        IbmsEnergyDeviceData data = new IbmsEnergyDeviceData();
                        data.setId(deviceId);
                        data.setIdentifier("12N");
                        data.setValue(value);
                        data.setTime(functionObj.getString("time"));
                        list.add(data);
                    }else if("12O".equals(functionObj.getString("identifier"))){
                        IbmsEnergyDeviceData data = new IbmsEnergyDeviceData();
                        data.setId(deviceId);
                        data.setIdentifier("12O");
                        data.setValue(value);
                        data.setTime(functionObj.getString("time"));
                        list.add(data);
                    }
                }
            }
            ibmsEnergyDeviceDataService.saveOrUpdateBatch(list);
        }

    }


    public void queryEnergyDay(){
        String year = DateUtils.parseDateToStr("yyyy-MM-dd", new Date());
        //电表有功电量
        ArrayList<String> list = new ArrayList<>();
        List<EnergyFlowRecord> ibmsEnergyDeviceDataList = new ArrayList<>();
        list.add("11X");
        EnergyFlowRecord ammeterValue = ibmsEnergyDeviceDataMapper.energyFlow(year,list);
        ammeterValue.setEnergyDeviceName("11X");
        if (ammeterValue == null){
            EnergyFlowRecord energyFlowRecord = new EnergyFlowRecord();
            energyFlowRecord.setEnergyDeviceName("11X");
            energyFlowRecord.setEnergyValue("0");
            ibmsEnergyDeviceDataList.add(energyFlowRecord);
        }else {
            ammeterValue.setEnergyDeviceName("11X");
            ibmsEnergyDeviceDataList.add(ammeterValue);
        }
        list.clear();
        list.add("12L");
        EnergyFlowRecord waterMeterValue = ibmsEnergyDeviceDataMapper.energyFlow(year,list);
        if (waterMeterValue == null){
            EnergyFlowRecord energyFlowRecord = new EnergyFlowRecord();
            energyFlowRecord.setEnergyDeviceName("12L");
            energyFlowRecord.setEnergyValue("0");
            ibmsEnergyDeviceDataList.add(energyFlowRecord);
        }else {
            waterMeterValue.setEnergyDeviceName("12L");
            ibmsEnergyDeviceDataList.add(waterMeterValue);
        }
        list.clear();
        list.add("12N");
        list.add("12O");
        EnergyFlowRecord airValue = ibmsEnergyDeviceDataMapper.energyFlow(year,list);
        if (airValue == null){
            EnergyFlowRecord energyFlowRecord = new EnergyFlowRecord();
            energyFlowRecord.setEnergyDeviceName("12N");
            energyFlowRecord.setEnergyValue("0");
            ibmsEnergyDeviceDataList.add(energyFlowRecord);
        }else {
            airValue.setEnergyDeviceName("12N");
            ibmsEnergyDeviceDataList.add(airValue);
        }
//        int countFlow = (int) ibmsEnergyFlowService.count();
//        List<IbmsEnergyFlow> listFlow = new ArrayList<>();
//        IbmsEnergyFlow ibmsEnergyFlow = new IbmsEnergyFlow();
//        if(countFlow == 0){
//            for (EnergyFlowRecord energyFlowRecord : ibmsEnergyDeviceDataList) {
//                if(energyFlowRecord.getEnergyDeviceName().equals("11X")){
//                   ibmsEnergyFlow.setEnergyDevice();
//                }
//            }
//        }

    }



}
